package com.bw.controller.orders;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.group.MPOrderitem;
import com.bw.entity.orders.PiOrderItemMaterial;
import com.bw.entity.orders.PiOrderItemProcess;
import com.bw.entity.process.PiProcess;
import com.bw.resp.ResponseResult;
import com.bw.service.orders.IPiOrderItemMaterialService;
import com.bw.service.orders.IPiOrderItemProcessService;
import com.bw.service.process.IPiProcessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-06-19
 */
@RestController
@RequestMapping("/order-material")
public class PiOrderItemMaterialController {

    @Autowired
    private IPiOrderItemMaterialService materialService;
    @Autowired
    private IPiOrderItemProcessService processService;

    @Autowired
    private IPiProcessService piProcessService;

    private final Logger logger=LoggerFactory.getLogger(PiOrderItemMaterialController.class);

    /**
     * @Description:保存工艺
     * @Author: 周明丽
     * @Date: 2020/7/1 19:28
     * @param orderitem:
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("saveGy")
    public ResponseResult saveGy(@RequestBody MPOrderitem orderitem){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            logger.info("开始添加工艺");
            List<PiOrderItemMaterial> materialList = orderitem.getMaterialList();
            for ( PiOrderItemMaterial m:materialList){
                materialService.save(m);
            }
            List<PiOrderItemProcess> processList = orderitem.getProcessList();
            for (PiOrderItemProcess p:processList){
                PiProcess process = piProcessService.getById(p.getProcessId());
                //准备工时费
                BigDecimal v = BigDecimal.valueOf(p.getPreWorkHour());
                BigDecimal pres = v.multiply(process.getPrePrice());
                p.setPreCost(pres);

                //单件工时费
                BigDecimal workhour = BigDecimal.valueOf(p.getSgWorkHour());
                BigDecimal sg = workhour.multiply(process.getPrice());
                p.setProCost(sg);
                processService.save(p);
            }
            result.setMessage("保存成功");
            logger.info("添加工艺结束");
        }catch (Exception e){
            result.setMessage("添加失败"+e.toString());
            result.setSuccess(false);
            logger.error("添加错误");
        }
        logger.info("添加工艺结束");
        return result;

    }
    /**
     * @Description:回显工艺制作
     * @Author: 周明丽
     * @Date: 2020/6/24 9:59
     * @param id:
     * @return: com.bw.resp.ResponseResult
     **/
    @GetMapping("getOrdPro/{id}")
    public ResponseResult saveGy(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        logger.info("回显工艺制作开始");
        try {
            MPOrderitem orderitem = new MPOrderitem();
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("orderItemId",id);
            List<PiOrderItemMaterial> materialList = materialService.list(queryWrapper);
            orderitem.setMaterialList(materialList);
            queryWrapper.eq("orderItemId",id);
            List<PiOrderItemProcess> processList = processService.list(queryWrapper);
            orderitem.setProcessList(processList);
            result.setResult(orderitem);
        }catch (Exception e){
            result.setMessage("回显工艺制作异常"+e.toString());
            result.setSuccess(false);
            logger.error("回显工艺制作异常");
        }
        logger.info("回显工艺制作结束");
        return result;
    }
}
